package com.example.control;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 本类用来演示:
 *
 * @Author 胡英俊
 * @Date 2022/7/12 20:01
 */
@RestController
@RequestMapping("client")
public class clientController {
    @Autowired
//    注意导的包
    private DiscoveryClient discoveryClient;
    @GetMapping("get")
    public String testClient(String serverName){
        List<ServiceInstance> instances = discoveryClient.getInstances(serverName);
        instances.forEach(System.out::println);
        ServiceInstance serviceInstance = instances.get(0);
        String host = serviceInstance.getHost();//ip
        int port = serviceInstance.getPort();//端口号
        System.out.println(host+""+port);
        return instances.get(0).toString();
    }
}
